/**
 * Copyright &copy; 2012-2016 civet All rights reserved.
 */
package cn.mcsys.myadmin.modules.air.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import cn.mcsys.myadmin.modules.air.service.AirRidePointService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.mcsys.myadmin.common.config.Global;
import cn.mcsys.myadmin.common.persistence.Page;
import cn.mcsys.myadmin.common.web.BaseController;
import cn.mcsys.myadmin.common.utils.StringUtils;
import cn.mcsys.myadmin.modules.air.entity.AirWorker;
import cn.mcsys.myadmin.modules.air.service.AirWorkerService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工作人员管理Controller
 * @author leopold
 * @version 2018-08-13
 */
@Controller
@RequestMapping(value = "${adminPath}/air/airWorker")
public class AirWorkerController extends BaseController {

	@Autowired
	private AirWorkerService airWorkerService;

	@Autowired
	private AirRidePointService airRidePointService;
	
	@RequiresPermissions("air:airWorker:view")
	@RequestMapping(value = {"list", ""})
	public String list(AirWorker airWorker, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<AirWorker> page = airWorkerService.findPage(new Page<AirWorker>(request, response), airWorker);
		model.addAttribute("page", page);
		List<AirRidePoint> ridePoints = airRidePointService.findAllList(new AirRidePoint());
		model.addAttribute("ridePoints",ridePoints);
		return "modules/air/airWorkerList";
	}

	@RequiresPermissions("air:airWorker:view")
	@RequestMapping(value = "form")
	public String form(AirWorker airWorker, Model model) {
	    if (airWorker.getId() != null){
            airWorker = airWorkerService.get(airWorker.getId());
        }
		List<AirRidePoint> ridePoints = airRidePointService.findAllList(new AirRidePoint());
		model.addAttribute("airWorker", airWorker);
		model.addAttribute("ridePoints",ridePoints);
		return "modules/air/airWorkerForm";
	}

	@RequiresPermissions("air:airWorker:edit")
	@RequestMapping(value = "save")
	public String save(AirWorker airWorker, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, airWorker)){
			return form(airWorker, model);
		}
		airWorkerService.save(airWorker);
//		addMessage(redirectAttributes, "保存工作人员成功");
		return "redirect:"+Global.getAdminPath()+"/air/airWorker/?repage";
	}
	
	@RequiresPermissions("air:airWorker:edit")
	@RequestMapping(value = "delete")
	public String delete(AirWorker airWorker, RedirectAttributes redirectAttributes) {
		airWorkerService.delete(airWorker);
		airWorker = airWorkerService.get(airWorker.getId());
		if(airWorker.getAirRidePoint() != null){
			AirRidePoint airRidePoint = airWorker.getAirRidePoint();
			airRidePoint.setWorkerSum(airRidePoint.getWorkerSum()-1);
			airRidePointService.updateTemplate(airRidePoint);
		}
//		addMessage(redirectAttributes, "删除工作人员成功");
		return "redirect:"+Global.getAdminPath()+"/air/airWorker/?repage";
	}

	@RequestMapping(value = "getDetail")
	@ResponseBody
	public Map<String,Object> getDetail(int id, int airRidePointId) {
		Map resultMap = new HashMap<String,Object>();
		AirWorker airWorker = airWorkerService.get(id);
		AirRidePoint airRidePoint = airRidePointService.get(airRidePointId);
		resultMap.put("airWorkerData",airWorker);
		resultMap.put("airRidePointData",airRidePoint);
		return resultMap;
	}

}